﻿module SpriteBuilder {

    export function buildAnimation(sprite: Phaser.Sprite) {
        sprite.animations.add('cast_forward', [0, 1, 2, 3, 4, 5, 6], 10, false);
        sprite.animations.add('cast_left', [13, 14, 15, 16, 17, 18, 19], 10, false);
        sprite.animations.add('cast_backward', [26, 27, 28, 29, 30, 31, 32], 10, false);
        sprite.animations.add('cast_right', [39, 40, 41, 42, 43, 44, 45], 10, false);

        sprite.animations.add('thrust_forward', [52, 53, 54, 55, 56, 57, 58, 59], 10, true);
        sprite.animations.add('thrust_left', [65, 66, 67, 68, 69, 70, 71, 72], 10, true);
        sprite.animations.add('thrust_backward', [78, 79, 80, 81, 82, 83, 84, 85], 10, true);
        sprite.animations.add('thrust_right', [91, 92, 93, 94, 95, 96, 97, 98], 10, true);

        sprite.animations.add('walk_forward', [104, 105, 106, 107, 108, 109, 110, 111, 112], 10, true);
        sprite.animations.add('walk_left', [117, 118, 119, 120, 121, 122, 123, 124, 125], 10, true);
        sprite.animations.add('walk_backward', [130, 131, 132, 133, 134, 135, 136, 137, 138], 10, true);
        sprite.animations.add('walk_right', [143, 144, 145, 146, 147, 148, 149, 150, 151], 10, true);

        sprite.animations.add('slash_forward', [156, 157, 158, 159, 160, 161], 10, false);
        sprite.animations.add('slash_left', [169, 170, 171, 172, 173, 174], 10, false);
        sprite.animations.add('slash_backward', [182, 183, 184, 185, 186, 187], 10, false);
        sprite.animations.add('slash_right', [195, 196, 197, 198, 199, 200], 10, false);

        sprite.animations.add('shoot_forward', [208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220], 10, false);
        sprite.animations.add('shoot_left', [221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233], 10, false);
        sprite.animations.add('shoot_backward', [234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246], 10, false);
        sprite.animations.add('shoot_right', [247, 248, 249, 250, 251, 252, 253, 254, 255, 256, 257, 258, 259], 10, false);

        sprite.animations.add('hurt', [260, 261, 262, 263, 264, 265], 10, false);
    }

    export function setSpritePhysics(sprite: Phaser.Sprite) {
        sprite.game.physics.p2.enable(sprite);
        sprite.game.physics.p2.enableBody(sprite, false);
        sprite.body.physicsBodyType = Phaser.Physics.P2JS;

        sprite.body.setRectangle(30, 55);

        sprite.body.fixedRotation = true;
        sprite.body.allowGravity = true;

        sprite.body.collideWorldBounds = true;
    }

    export function getSpriteName(sprite: Phaser.Sprite): string {
        var enemyKeys = ['Skeleton', 'ArmoredOrc'];
        var enemyNames = ['Skeleton', 'Armored Orc'];

        var keyIndex = enemyKeys.indexOf(sprite.key.toString());
        return enemyNames[keyIndex];
    }
}